package org.boot.easyapi.core.util.sql;

import org.boot.easyapi.util.CollUtil;
import org.boot.easyapi.util.StrUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字段映射工具类
 */
public class MappingConvertUtil {

    /**
     * 映射表结构字段
     * @param paramMap
     * @return
     */
    public static Map<String, Object> convertMap(Map<String, Object> paramMap) {
        return mappingFiled(paramMap);
    }

    /**
     * 映射请求参数
     * @param paramMap
     * @return
     */
    public static Map<String, Object> convertParamMap(Map<String, Object> paramMap) {
        return mappingParam(paramMap);
    }





    /**
     * 查询字段驼峰映射
     * @param filedList
     * @return
     */
    public static String mappingFiled(List<String> filedList){
        if(CollUtil.isEmpty(filedList)) return "*";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < filedList.size(); i++) {
            String field = filedList.get(i);
            String mappedField = mappingResponse(field);
            sb.append(field).append(" AS ")
                    .append("\"")
                    .append(mappedField)
                    .append("\"");
            // 如果不是最后一个元素，添加逗号和空格
            if (i != filedList.size() - 1) {
                sb.append(", ");
            }
        }
        return sb.toString();
    }




    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~识别特殊判定符号 @ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    /**
     * 对象入参字段映射
     * @param filedMap
     * @return
     */
    public static Map<String, Object> mappingFiled(Map<String, Object> filedMap){
        if(CollUtil.isEmpty(filedMap)) return filedMap;
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<String, Object> entry : filedMap.entrySet()) {
            //忽略@逻辑入参
            if(entry.getKey().contains("@")){
                result.put(entry.getKey(), entry.getValue());
                continue;
            }
            result.put(mappingJoinIn(entry.getKey()), entry.getValue());
        }
        return result;
    }

    /**
     * 请求参数字段映射
     * @param paramMap
     * @return
     */
    public static Map<String, Object> mappingParam(Map<String, Object> paramMap){
        if(CollUtil.isEmpty(paramMap)) return paramMap;
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
            //忽略@逻辑入参
            if(entry.getKey().contains("@")){
                result.put(entry.getKey(), entry.getValue());
                continue;
            }
            result.put(mappingJoinIn(entry.getKey()), entry.getValue());
        }
        return result;
    }



    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~单个转换~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
    /**
     * 下划线转小驼峰（user_name → userName）
     * @param field
     * @return
     */
    public static String mappingResponse(String field) {
        if (StrUtil.isEmpty(field))  return field;
        StringBuilder sb = new StringBuilder();
        String[] parts = field.split("_");
        for (int i = 0; i < parts.length;  i++) {
            if (i == 0) {
                sb.append(parts[i]);
            } else {
                sb.append(Character.toUpperCase(parts[i].charAt(0)))
                        .append(parts[i].substring(1));
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰命名转下划线命名（userName → user_name）
     * @param field
     * @return
     */
    public static String mappingJoinIn(String field) {
        if (StrUtil.isEmpty(field))  {
            return field;
        }
        // 首字母单独处理，后续字母按原逻辑转换
        String firstChar = field.substring(0,  1);
        String remaining = field.substring(1)
                .replaceAll("(?=[A-Z])", "_")
                .toLowerCase();
        return firstChar + remaining;
    }


    // 测试用例
//    public static void main(String[] args) {
////        System.out.println(mappingResponse("User_name"));     // 输出 userName
////        System.out.println(mappingJoinIn("UserNameAgeCountsaddwew"));      // 输出 user_name
//        List<String> list = CollUtil.newArrayList("user_name", "user_age", "user_counts");
//        System.out.println(mappingFiled(list));
//
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("userName", "许子瑶");
//        map.put("userAge", "23");
//        map.put("userCounts", "100255");
//        System.out.println(mappingFiled(map));
//
//
//
//    }

}
